<?php
/**
 * Free Uch
 *
 * @category   FreeUch
 * @copyright  Copyright 2009 lordharrdlee(http://www.lordharrdlee.cn)
 * @author     lordharrdlee<lordharrdlee@gmail.com>
 * @version    $Id: Hotel.php 10 2010-01-14 10:31:01Z lordharrdlee $
 */
/**
 * Model_Hotel
 * @author liyizhou<lordharrdlee@hotmail.com>
 */
class Model_Hotel {

    public function getFile($script,$data,$nocache = 0,$time=864000) {
        $query = '';
        if(!empty($data) && is_array($data)) {
            foreach($data as $key => $value) {
                $path .= '_'.$key.'_'.$value;
                $query .= '&'.$key.'='.$value;
            }
        }
        if($nocache) {
            $path = '';
        } else {
            $path = FREEUCH_ROOT.'data/hotel/'.$script.'/'.$script.$path.'.data';
        }
        if($path!=''&&file_exists($path)) {
            $t=time()-filemtime($path);
        }else {
            $t=0;
        }
        if($t!=0&&$t<$time) {
            return file_get_contents($path);
        }else {
            $back=file_get_contents("http://un.zhuna.cn/api/json/".$script.".asp?u=276278&m=f40aafae344e2004".$query);
            if($path != '' && $back != '')file_put_contents($path,$back);
            return $back;
        }
    }

    public function getPage($total,$ListUrl,$PageIndex=1,$n=10) {
        if($PageIndex<1)$PageIndex=1;
        $maxPg=ceil($total/$n);
        if($maxPg<1)return "Sorry:没有数据";
        if($maxPg==1)return;
        $p1=floor(($PageIndex-1)/$n)*$n+1;
        $p2=(($p1+$n)>$maxPg)?$maxPg+1:($p1+$n);
        $rhtml="";
        if($p1>1) {
            $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array(1,'1',0,''),$ListUrl);
            $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array($p1-1,'<<',$total,''),$ListUrl);
        }
        for($i=$p1;$i<$p2;$i++) {
            if($PageIndex==$i) {
                $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array($i,$i,$total,' id=pg_current'),$ListUrl);
            }else {
                $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array($i,$i,$total,''),$ListUrl);
            }

        }
        if($p2<$maxPg) {
            $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array($p2,'>>',$total,''),$ListUrl);
            $rhtml.=str_replace(array('{p}','{t}','{n}','{b}'),array($maxPg,$maxPg,0,''),$ListUrl);
        }
        return $rhtml;
    }

    public function getRecommoned($cid) {
        $path=FREEUCH_ROOT."data/hotel/tuijian/tj_".$cid.".data";
        if(file_exists($path)) {
            $t=time()-filemtime($path);
        }else {
            $t=0;
        }
        if($t!=0&&$t<9999999) {
            return json_decode(file_get_contents($path));
        }else {
            $strjson=$this->getFile('search',array('cityid'=>$cid), 1);
            $hs=json_decode($strjson);
            $n=(count($hs->hotels)>5)?5:count($hs->hotels);
            for($i=0;$i<$n;$i++) {
                $h=$hs->hotels[$i];
                $th='';
                $th->id=$h->id;
                $th->Name=$h->Name;
                $th->Picture=$h->Picture;
                $tj1[]=$th;

            }
            $strjson=$this->getFile('search',array('cityid'=>$cid,'maxprice'=>180,'px'=>2),1);
            $hs=json_decode($strjson);
            $n=(count($hs->hotels)>8)?8:count($hs->hotels);
            for($i=0;$i<$n;$i++) {
                $h=$hs->hotels[$i];
                $th='';
                $th->id=$h->id;
                $th->Name=$h->Name;
                $th->mjiage=$h->mjiage;
                $tj2[]=$th;

            }
            $strjson=$this->getFile('search',array('cityid'=>$cid,'minprice'=>180,'maxprice'=>240,'px'=>2),1);
            $hs=json_decode($strjson);
            $n=(count($hs->hotels)>8)?8:count($hs->hotels);
            for($i=0;$i<$n;$i++) {
                $h=$hs->hotels[$i];
                $th='';
                $th->id=$h->id;
                $th->Name=$h->Name;
                $th->mjiage=$h->mjiage;
                $tj3[]=$th;

            }
            $strjson=$this->getFile('search',array('cityid'=>$cid,'minprice'=>240,'maxprice'=>400,'px'=>2),1);
            $hs=json_decode($strjson);
            $n=(count($hs->hotels)>8)?8:count($hs->hotels);
            for($i=0;$i<$n;$i++) {
                $h=$hs->hotels[$i];
                $th='';
                $th->id=$h->id;
                $th->Name=$h->Name;
                $th->mjiage=$h->mjiage;
                $tj4[]=$th;

            }
            $tuijian[]=$tj1;
            $tuijian[]=$tj2;
            $tuijian[]=$tj3;
            $tuijian[]=$tj4;
            $file=json_encode($tuijian);
            file_put_contents($path,$file);
            return $tuijian;
        }
    }
}